import 'package:flutter/material.dart';
import 'package:get/get.dart';
import 'package:get_storage/get_storage.dart';
import 'package:test1/xykk/demo/dialog/MyDialog.dart';
import 'MyNavigatorObserver.dart';
import 'RouteConfig/Route.dart';
import 'app_theme.dart';
import 'test_getx/Test_service/ApiService.dart';
import 'test_getx/Test_service/StorageService.dart';
import 'app_translation.dart';

void main() async {
  await GetStorage.init();  // 初始化 GetStorage
  await initServices();
  runApp(MyApp());
}
Future<void> initServices() async {
  print("初始化服务");
  await Get.putAsync(() async =>  ApiService());
  await Get.putAsync(() async =>  StorageService());
  print("所有服务启动");
}
/// 加载语言
/// 如果[appLocal]有配置的语言则使用已配置的语言
/// 如果[appLocal]没有配置则判断系统语言是否有支持的语言
/// 如果APP所支持的语言不包含当前系统所使用的语言则默认使用'zh_CN'
Locale? localeResolutionCallback(
    Locale? locale, Iterable<Locale>? supportedLocales) {
  return Get.locale;
}

class MyApp extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return GetMaterialApp(
      // theme: AppTheme.dark(),
      localeResolutionCallback: localeResolutionCallback,
      // darkTheme: AppTheme.light(),
      // themeMode: ThemeMode.system,
      // localizationsDelegates: [
      //   // 本地化的代理类
      //   GlobalMaterialLocalizations.delegate,
      //   GlobalWidgetsLocalizations.delegate,
      // ],
      title: "GetX",
      translations: AppTranslationService(),
      home: Scaffold(
        // appBar: AppBar(title: const Text("首页:GetX Title")),
        body: RouterLable(),
      ),
      initialRoute: '/',
      // defaultTransition: Transition.upToDown,
      getPages: Routes,
      navigatorObservers: [MyNavigatorObserver()],
    );
  }
}

class RouterLable extends StatelessWidget {
  RouterLable({super.key});

  var _routes = [];

  @override
  StatelessElement createElement() {
    return super.createElement();
  }

  @override
  Widget build(BuildContext context) {

    _routes = Routes.where((route) {
      var count = 0;
      for (int i = 0; i < route.name.length; i++) {
        if (route.name[i] == '/') {
          count++;
        }
      }
      if (count == 1) {
        return true;
      } else {
        return false;
      }
    }).toList();
    // print('Jt:$_routes');
    return ListView(
      children: _routes
          .map((route) => ElevatedButton(
              onPressed: ()async {
                if (route.name == '/') {
                }
                Get.toNamed(route.name, parameters: {'from': route.name});
              },
              child: Text(route.name)))
          .toList(),
    );
  }
}
